/**
 * Created by zhoua on 2017/8/10.
 */
import { getUserInfo, queryBalance } from 'api';
import * as types from '../types';

const state = {
  role: 'operator', // operator or user
  name: '',
  phone: '', // 电话
  wx: '',
  openId: '', // 微信用户标识 o0An7wWZKb-_NSr4OsL_Vha8lVM4
  type: '', // 用户类型
  id: '', // 数据id
  account: '', // 账号
  password: '', // 密码
  salt: '', // md5密码钥
  birthday: '', // 名字
  sex: '', // 性别（1：男 2：女）
  email: '', // 电子邮件
  roleId: '', // 角色id
  status: '', // 状态(1：启用  2：冻结  3：删除）
  createtime: '', // 创建时间
  updatetime: '', // 更新时间
  avatar: '', // 头像
  operatingAmount: 0,
  address: {
    result: '',
    addressComponent: {
      province: '',
      nation: '',
      city: '',
      street: '',
      district: '',
      streetNumber: ''
    },
    location: {
      lng: '',
      lat: ''
    }
  }
};

const actions = {
  getUserInfo ({ commit }) {
    return new Promise((resolve, reject) => {
      getUserInfo({
        openid: state.openId
      })
        .then(response => {
          if (!response.resultData) {
            weui.topTips('未获取到用户信息!');
          }
          commit(types.GET_USERINFO, response.resultData);
          if (response.resultData.id) {
            localStorage.setItem('userId', response.resultData.id);
            localStorage.setItem('openId', response.resultData.openid);
            resolve(response.resultData.id);
          } else {
            return false;
          }
        })
    })
  },
  getUserId ({ commit }) {
    commit(types.UPDATE_USERID);
  }
}

const mutations = {
  [types.GET_USERINFO] (state, obj) {
    state.id = obj.id;
    state.type = obj.type;
    state.role = obj.type;
    // state.openId = obj.openid;
    state.avatar = obj.avatar;
    state.account = obj.account;
    state.password = obj.passwor;
    state.salt = obj.salt;
    state.name = obj.name;
    state.birthday = obj.birthda;
    state.sex = obj.sex === 1 ? '男' : '女';
    state.email = obj.email;
    state.phone = obj.phone;
    state.roleId = obj.roleid;
    state.status = obj.status;
    state.createtime = obj.createtime;
    state.updatetime = obj.updatetime;
  },
  [types.UPDATE_USERID] (state) {
    state.id = localStorage.getItem('userId');
    // state.openId = localStorage.getItem('openId');
  },
  [types.UPDATE_OPENID] (state, openid) {
    state.openId = openid;
  },
  [types.UPDATE_AMOUNT] (state, amount) {
    state.operatingAmount = amount;
  },
  [types.UPDATE_ADDRESS] (state, result) {
    state.address.result = result.result;
    state.address.addressComponent.province = result.address_component.province;
    state.address.addressComponent.nation = result.address_component.nation;
    state.address.addressComponent.city = result.address_component.city;
    state.address.addressComponent.street = result.address_component.street;
    state.address.addressComponent.district = result.address_component.district;
    state.address.addressComponent.streetNumber = result.address_component.streetNumber;
    state.address.location.lng = result.location.lng;
    state.address.location.lat = result.location.lat;
  }
}

export default {
  state,
  actions,
  mutations
}
